#include "stdafx.h"
#include "TGRxImplClass.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

TGRX_IMPLEMENT_DYNCREATE(TGRxImplClass,TGRxClass,MDLCURRENTVER,0,	\
						 _T("TGRxKernel.dll|") TGRX_CLASS_APP)

TGRxImplClass::TGRxImplClass()
{
	m_pszClassName = NULL;
	m_pszParentClassName = NULL;
	m_pszAppName = NULL;
	m_nMDLVer = 0;
	m_nClassVer = 0;
	m_nProxyFlag = -1;
	m_pMakeClassObjFunc = NULL;
}

TGRxImplClass::~TGRxImplClass()
{
	if (m_pszClassName != NULL)
	{
		delete [] m_pszClassName;
	}

	if (m_pszParentClassName != NULL)
	{
		delete [] m_pszParentClassName;
	}

	if (m_pszAppName != NULL)
	{
		delete [] m_pszAppName;
	}

	freeBuffer();
}

void TGRxImplClass::setClassName(const char * pszClassName)
{
	if (pszClassName == NULL) return;
	if (m_pszClassName != NULL)
	{
		delete [] m_pszClassName;
	}
	
	int nLen = strlen(pszClassName);
	m_pszClassName = new char[nLen + 1];
	if(m_pszClassName != NULL)
	{
		strcpy(m_pszClassName,pszClassName);
	}
}

const char * TGRxImplClass::className()
{
	return m_pszClassName;
}

void TGRxImplClass::setParentClassName(const char * pszParentClassName)
{
	if (m_pszParentClassName != NULL)
	{
		delete [] m_pszParentClassName;
	}

	if (pszParentClassName != NULL)
	{
		int nLen = strlen(pszParentClassName);
		m_pszParentClassName = new char[nLen + 1];

		if (m_pszParentClassName != NULL)
		{
			strcpy(m_pszParentClassName,pszParentClassName);
		}
	}
}

const char * TGRxImplClass::parentClassName()
{
	return m_pszParentClassName;
}

void TGRxImplClass::setMDLVer(int nMDLVer)
{
	m_nMDLVer = nMDLVer;
}

int	TGRxImplClass::mdlVer()
{
	return m_nMDLVer;
}

void TGRxImplClass::setClassVer(int nClassVer)
{
	m_nClassVer = nClassVer;
}

int	TGRxImplClass::classVer()
{
	return m_nClassVer;
}

void TGRxImplClass::setProxyFlags(int nFlags)
{
	m_nProxyFlag = nFlags;
}

int TGRxImplClass::proxyFlags()
{
	return m_nProxyFlag;
}

void TGRxImplClass::setMakeClassObjFunc(makeObjCallBackFunc pFunc)
{
	m_pMakeClassObjFunc = pFunc;
}

makeObjCallBackFunc TGRxImplClass::makeClassObjFunc()
{
	return m_pMakeClassObjFunc;
}

void TGRxImplClass::setAppName(const char * pszAppName)
{
	if (pszAppName == NULL) return;
	if (m_pszAppName != NULL)
	{
		delete [] m_pszAppName;
	}

	int nLen = strlen(pszAppName);
	m_pszAppName = new char[nLen + 1];
	ASSERT(m_pszAppName != NULL);

	strcpy(m_pszAppName, pszAppName);
}

const char * TGRxImplClass::appName()
{
	return m_pszAppName;
}

TGRxObject*	TGRxImplClass::addX(TGRxClass* pRxClass, TGRxObject* pRxObject)
{
	if (pRxClass == NULL) return NULL;
	TGRxObject * pRxOldObj = getX(pRxClass);
	if (pRxOldObj == NULL)
	{
		m_mapRxClass2RxObj.SetAt(pRxClass,pRxObject);
	}

	return pRxOldObj;
}

TGRxObject*	TGRxImplClass::getX(TGRxClass* pRxClass)
{
	if (pRxClass == NULL) return NULL;
	TGRxObject * pRxObject = NULL;
	m_mapRxClass2RxObj.Lookup((void *)pRxClass,(void *&)pRxObject);
	return pRxObject;
}

TGRxObject*	TGRxImplClass::delX(TGRxClass* pRxClass)
{
	if (pRxClass == NULL) return NULL;
	TGRxObject * pRxOldObj = getX(pRxClass);
	if (pRxOldObj != NULL)
	{
		m_mapRxClass2RxObj.RemoveKey((void *)pRxClass);
	}
	return pRxOldObj;
}

TGRxObject*	TGRxImplClass::queryX(TGRxClass* pProtocolClass)
{	
	if (pProtocolClass == NULL) return NULL;
	TGRxClass * pRxClass = NULL;
	TGRxObject * pProtocolClassObj = NULL;
	
	for(POSITION pos = m_mapRxClass2RxObj.GetStartPosition();pos != NULL;)
	{
		m_mapRxClass2RxObj.GetNextAssoc(pos,(void *&)pRxClass,(void *&)pProtocolClassObj);
		if (pProtocolClassObj != NULL && pRxClass == pProtocolClass)
		{
			return pProtocolClassObj;
		}
	}
	return NULL;	
}

TGRxObject*	TGRxImplClass::create()
{
	TGRxObject * pNewObject = NULL;
	if (m_pMakeClassObjFunc != NULL)
	{
		pNewObject = (* m_pMakeClassObjFunc)();
	}
	return pNewObject;
}

const char*	TGRxImplClass::appName() const
{
	return m_pszAppName;
}

const char*	TGRxImplClass::name() const
{
	return m_pszClassName;
}

void TGRxImplClass::getClassVersion(int& nMdlVer,int& nClassVer) const
{
	nMdlVer   = m_nMDLVer;
	nClassVer = m_nClassVer;
}

int	TGRxImplClass::proxyFlags() const
{
	return m_nProxyFlag;
}

BOOL TGRxImplClass::isDerivedFrom(const TGRxClass* pRxClass) const
{
	TGRxClass* pRxParent = myParent();
	return (pRxParent == pRxClass);
}

TGRxClass* TGRxImplClass::myParent() const
{
	TGRxClass * pParentRxClass = (TGRxClass*)((TGRxDictionary*)rxSysRegistry()
		->at(TGRX_CLASS_DICTIONARY))->at(m_pszParentClassName);
	return pParentRxClass;
}

void TGRxImplClass::freeBuffer()
{
	TGRxClass * pRxClass = NULL;
	TGRxObject * pRxObj = NULL;
	for(POSITION pos = m_mapRxClass2RxObj.GetStartPosition();pos != NULL;)
	{
		m_mapRxClass2RxObj.GetNextAssoc(pos,(void *&)pRxClass,(void *&)pRxObj);
		if (pRxObj != NULL)
			delete pRxObj;
	}
	m_mapRxClass2RxObj.RemoveAll();
}
